package com.xjtu.duansheli.controller;

import com.xjtu.duansheli.entity.UserInfo;
import com.xjtu.duansheli.json.AjaxResponse;
import com.xjtu.duansheli.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 漩涡鸣人
 * @since 2020-10-24
 */
@Slf4j
@RestController
@RequestMapping("/project/user-info")
@Api(value = "用户信息管理模块", description = "这是用户信息增删改查的模块")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;


    @ApiOperation(value = "获取全部用户", notes = "获取所有用户的信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", required = false, defaultValue = "1", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "size", value = "每页数量", required = false, defaultValue = "10", paramType = "query", dataType = "int")
    })
    @RequestMapping(value = "/getUsers", method = RequestMethod.GET)
    public AjaxResponse getAll(@RequestParam(value = "current") Integer current,
                               @RequestParam(value = "size") Integer size) {

        return AjaxResponse.success(userInfoService.getAllUser(current,size));
    }


    @ApiOperation(value = "根据id查找用户", notes = "获取根据id查找到的用户")
    @ApiImplicitParam(name = "uid", value = "用户id", required = true, paramType = "query", dataType = "Long")
    @RequestMapping(value = "/getUserById", method = RequestMethod.GET)
    public AjaxResponse getUserById(@RequestParam(name = "uid") Long uid) {
        return AjaxResponse.success(userInfoService.getUserByid(uid));
    }

    @ApiOperation(value = "根据手机号查找用户", notes = "获取根据手机号查找到的用户")
    @ApiImplicitParam(name = "phone", value = "手机号", required = true, paramType = "query", dataType = "String")
    @RequestMapping(value = "/getUserByPhone", method = RequestMethod.GET)
    public AjaxResponse getUserByPhone(@RequestParam(name = "phone") String phone) {
        return AjaxResponse.success(userInfoService.getUserByPhone(phone));
    }

    @ApiOperation(value = "添加用户信息", notes = "获取用户信息并且添加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uName", value = "用户名", required = true, paramType = "form", dataType = "String"),
            @ApiImplicitParam(name = "gender", value = "用户性别", required = true, paramType = "form", dataType = "String"),
            @ApiImplicitParam(name = "wechat", value = "用户微信", required = true, paramType = "form", dataType = "String"),
            @ApiImplicitParam(name = "phone", value = "用户手机号", required = true, paramType = "form", dataType = "String"),
            @ApiImplicitParam(name = "baiduId", value = "用户百度id", required = true, paramType = "form", dataType = "Long")
    })
    @RequestMapping(value = "/addUser", method = RequestMethod.POST)
    public AjaxResponse addUser(@RequestParam(value = "uName") String uName,
                                @RequestParam(value = "gender") String gender,
                                @RequestParam(value = "wechat") String wechat,
                                @RequestParam(value = "phone") String phone,
                                @RequestParam(value = "baiduId") Long baiduId) {
        UserInfo user = new UserInfo()
                .setUname(uName)
                .setGender(gender)
                .setWechat(wechat)
                .setPhone(phone)
                .setBaiduId(baiduId);
        return AjaxResponse.success("数据库受影响行数->" + userInfoService.addUser(user));
    }

    @ApiOperation(value = "修改用户信息", notes = "录入新的用户信息并且修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uName", value = "用户名", required = false, paramType = "form", dataType = "String"),
            @ApiImplicitParam(name = "gender", value = "用户性别", required = false, paramType = "form", dataType = "String"),
            @ApiImplicitParam(name = "wechat", value = "用户微信", required = false, paramType = "form", dataType = "String"),
            @ApiImplicitParam(name = "phone", value = "用户手机号", required = false, paramType = "form", dataType = "String")
    })
    @RequestMapping(value = "/updateUser", method = RequestMethod.PUT)
    public AjaxResponse updateUser(@RequestParam(value = "uName") String uName,
                                   @RequestParam(value = "gender") String gender,
                                   @RequestParam(value = "wechat") String wechat,
                                   @RequestParam(value = "phone") String phone,
                                   HttpServletRequest request) {

        UserInfo loginUser = (UserInfo) request.getSession().getAttribute("loginUser");
        UserInfo user = new UserInfo()
                .setUid(loginUser.getUid()) //从session中获取已登录用户，此处测试用假数据
//                .setUid(1319909565211205634L)
                .setUname(uName)
                .setGender(gender)
                .setWechat(wechat)
                .setPhone(phone);
        return AjaxResponse.success("数据库受影响行数->" + userInfoService.updateUser(user));
    }

    @ApiOperation(value = "删除用户信息", notes = "从数据库中删除某个用户")
    @ApiImplicitParam(name = "uid", value = "删除用户id", required = true, paramType = "query", dataType = "Long")
    @RequestMapping(value = "/deleteUser", method = RequestMethod.DELETE)
    public AjaxResponse deleteUser(@RequestParam(value = "uid") Long uid) {
        return AjaxResponse.success("数据库受影响行数->" + userInfoService.deleteUser(uid));
    }

    @ApiOperation(value = "用户总数量", notes = "获取数据库中用户总数量")
    @RequestMapping(value = "/getTotal", method = RequestMethod.GET)
    public AjaxResponse getTotal() {
        return AjaxResponse.success(userInfoService.getTotal());
    }

}

